﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Caliburn.Micro;
using Microsoft.Phone.Tasks;
using FuelTracker.ViewModels;
using Microsoft.Phone.Controls;
using System.Collections.Generic;
using FuelTracker.Services;
using System.Diagnostics;
using FuelTracker.DataAccess;

namespace FuelTracker.Framework
{
    public class Bootstrapper : PhoneBootstrapper
    {
        PhoneContainer container;


        protected override void Configure()
        {
            container = new PhoneContainer(this);

            container.RegisterPhoneServices();
            container.RegisterAllViewModelsForPages();
            container.RegisterSingleton<IStorageService, StorageService>();
            container.RegisterSingleton<ParameterService, ParameterService>();
            container.RegisterSingleton<IVehicleDao, IsolatedStorageVehicleDao>();

            //container.InstallChooser<PhoneNumberChooserTask, PhoneNumberResult>();
            //container.InstallLauncher<EmailComposeTask>();

            AddCustomConventions();
        }

        /*protected override void Configure()
        {
            container = new PhoneContainer(this);


            container.RegisterPerRequest(typeof(MainViewModel), "MainViewModel", typeof(MainViewModel));
            container.RegisterPerRequest(typeof(CreateVehicleViewModel), "CreateVehicleViewModel", typeof(CreateVehicleViewModel));
            container.RegisterPerRequest(typeof(FuelLogsViewModel), "FuelLogsViewModel", typeof(FuelLogsViewModel));
            container.RegisterPerRequest(typeof(CreateFuelLogViewModel), "CreateFuelLogViewModel", typeof(CreateFuelLogViewModel));
            container.RegisterSingleton(typeof(IStorageService), null, typeof(StorageService));
            container.RegisterSingleton(typeof(ParameterService), null, typeof(ParameterService));

            container.RegisterInstance(typeof(INavigationService), null, new FrameAdapter(RootFrame));
            container.RegisterInstance(typeof(IPhoneService), null, new PhoneApplicationServiceAdapter(PhoneService));


            //container.Activator.InstallChooser<PhoneNumberChooserTask, PhoneNumberResult>();
            //container.Activator.InstallLauncher<EmailComposeTask>();

            AddCustomConventions();
        }*/

        static void AddCustomConventions()
        {
            ConventionManager.AddElementConvention<Pivot>(Pivot.ItemsSourceProperty, "SelectedItem", "SelectionChanged").ApplyBinding =
                (viewModelType, path, property, element, convention) =>
                {
                    if (ConventionManager
                        .GetElementConvention(typeof(ItemsControl))
                        .ApplyBinding(viewModelType, path, property, element, convention))
                    {
                        ConventionManager
                            .ConfigureSelectedItem(element, Pivot.SelectedItemProperty, viewModelType, path);
                        ConventionManager
                            .ApplyHeaderTemplate(element, Pivot.HeaderTemplateProperty, viewModelType);
                        return true;
                    }

                    return false;
                };

            ConventionManager.AddElementConvention<Panorama>(Panorama.ItemsSourceProperty, "SelectedItem", "SelectionChanged").ApplyBinding =
                (viewModelType, path, property, element, convention) =>
                {
                    if (ConventionManager
                        .GetElementConvention(typeof(ItemsControl))
                        .ApplyBinding(viewModelType, path, property, element, convention))
                    {
                        ConventionManager
                            .ConfigureSelectedItem(element, Panorama.SelectedItemProperty, viewModelType, path);
                        ConventionManager
                            .ApplyHeaderTemplate(element, Panorama.HeaderTemplateProperty, viewModelType);
                        return true;
                    }

                    return false;
                };
        }

        protected override object GetInstance(Type service, string key)
        {
            return container.GetInstance(service, key);
        }

        protected override IEnumerable<object> GetAllInstances(Type service)
        {
            return container.GetAllInstances(service);
        }

        protected override void BuildUp(object instance)
        {
            container.BuildUp(instance);
        }
    }

}
